
#include <iostream>
#include <sstream>
#include <iomanip>

#include <TimeDate.h>

namespace AirlineRes
{

TimeDate::TimeDate
(
  int _year,
  int _month,
  int _day,
  int _hour,
  int _minute
)
{
  SetTime( _year, _month, _day, _hour, _minute );
}

TimeDate::TimeDate( std::string const& _ddmmyyyy )
{
  SetTime( _ddmmyyyy );
}

TimeDate::TimeDate( std::string const& _ddmmyyyy, std::string const& _hhmm )
{
  SetTime( _ddmmyyyy, _hhmm );
}

void TimeDate::SetTime
(
  int _year,
  int _month,
  int _day,
  int _hour,
  int _minute
)
{
  // Create tm Time struct and convert to time_t for storage.
  time_t tTime;
  time ( &tTime );
  tm* tmTime = gmtime( &tTime );
  tmTime->tm_min  = _minute;
  tmTime->tm_hour = _hour;
  tmTime->tm_mday = _day;
  tmTime->tm_mon  = _month - 1;
  tmTime->tm_year = _year - 1900;
  m_time = mktime( tmTime );
}

void TimeDate::SetToCurrentTime()
{
  // Create tm Time struct and convert to time_t for storage.
  time_t tTime;
  time ( &tTime );
  tm* tmTime = gmtime( &tTime );
  m_time = mktime( tmTime );
}

void TimeDate::SetTime
(
  std::string const& _ddmmyyyy
)
{
  int const day   = atoi( _ddmmyyyy.substr( 0, 2 ).c_str() );
  int const month = atoi( _ddmmyyyy.substr( 2, 2 ).c_str() );
  int const year  = atoi( _ddmmyyyy.substr( 4, 4 ).c_str() );
  SetTime( year, month, day );
}

void TimeDate::SetTime
(
  std::string const& _ddmmyyyy,
  std::string const& _hhmm
)
{
  int const day   = atoi( _ddmmyyyy.substr( 0, 2 ).c_str() );
  int const month = atoi( _ddmmyyyy.substr( 2, 2 ).c_str() );
  int const year  = atoi( _ddmmyyyy.substr( 4, 4 ).c_str() );

  int const hour   = atoi( _hhmm.substr( 0, 2 ).c_str() );
  int const minute = atoi( _hhmm.substr( 2, 2 ).c_str() );

  SetTime( year, month, day, hour, minute );
}


tm* TimeDate::GetTm() const
{
  return gmtime( &m_time );
}

int TimeDate::GetMinute() const
{
  tm* tmTime = GetTm();
  return tmTime->tm_min;
}

int TimeDate::GetHour() const
{
  tm* tmTime = GetTm();
  return tmTime->tm_hour;
}

int TimeDate::GetDay() const
{
  tm* tmTime = GetTm();
  return tmTime->tm_mday;
}

int TimeDate::GetMonth() const
{
  tm* tmTime = GetTm();
  return tmTime->tm_mon + 1;
}

int TimeDate::GetYear() const
{
  tm* tmTime = GetTm();
  return tmTime->tm_year + 1900;
}

std::string TimeDate::GetISODate() const
{
  std::string s;
  std::stringstream ss;
  ss << std::setfill( '0' )
     << std::setw( 4 )
     << GetYear()
     << std::setw( 2 )
     << GetMonth()
     << std::setw( 2 )
     << GetDay();
  return ss.str();
}

std::string TimeDate::GetDDMMYYYY() const
{
  std::stringstream ss;
  ss << std::setfill( '0' )
     << std::setw( 2 )
     << GetDay()
     << std::setw( 2 )
     << GetMonth()
     << std::setw( 4 )
     << GetYear();
  return ss.str();
}

std::string TimeDate::GetHHMM() const
{
  std::stringstream ss;
  ss << std::setfill( '0' )
     << std::setw( 2 )
     << GetHour()
     << std::setw( 2 )
     << GetMinute();
  return ss.str();
}


} // namespace AirlineRes
